/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.applicatioN.controllers;

import com.applicatioN.controllers.exceptions.NonexistentEntityException;
import java.io.Serializable;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import com.applicatioN.entities.Bureau;
import com.applicatioN.entities.Candidat;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;

/**
 *
 * @author Asus_k53s
 */
public class CandidatJpaController implements Serializable {

    public CandidatJpaController(EntityManagerFactory emf) {
        this.emf = emf;
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(Candidat candidat) {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Bureau nationalite = candidat.getNationalite();
            if (nationalite != null) {
                nationalite = em.getReference(nationalite.getClass(), nationalite.getPays());
                candidat.setNationalite(nationalite);
            }
            em.persist(candidat);
            if (nationalite != null) {
                nationalite.getCandidatCollection().add(candidat);
                nationalite = em.merge(nationalite);
            }
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Candidat candidat) throws NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Candidat persistentCandidat = em.find(Candidat.class, candidat.getIdentifiant());
            Bureau nationaliteOld = persistentCandidat.getNationalite();
            Bureau nationaliteNew = candidat.getNationalite();
            if (nationaliteNew != null) {
                nationaliteNew = em.getReference(nationaliteNew.getClass(), nationaliteNew.getPays());
                candidat.setNationalite(nationaliteNew);
            }
            candidat = em.merge(candidat);
            if (nationaliteOld != null && !nationaliteOld.equals(nationaliteNew)) {
                nationaliteOld.getCandidatCollection().remove(candidat);
                nationaliteOld = em.merge(nationaliteOld);
            }
            if (nationaliteNew != null && !nationaliteNew.equals(nationaliteOld)) {
                nationaliteNew.getCandidatCollection().add(candidat);
                nationaliteNew = em.merge(nationaliteNew);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = candidat.getIdentifiant();
                if (findCandidat(id) == null) {
                    throw new NonexistentEntityException("The candidat with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Candidat candidat;
            try {
                candidat = em.getReference(Candidat.class, id);
                candidat.getIdentifiant();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The candidat with id " + id + " no longer exists.", enfe);
            }
            Bureau nationalite = candidat.getNationalite();
            if (nationalite != null) {
                nationalite.getCandidatCollection().remove(candidat);
                nationalite = em.merge(nationalite);
            }
            em.remove(candidat);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Candidat> findCandidatEntities() {
        return findCandidatEntities(true, -1, -1);
    }

    public List<Candidat> findCandidatEntities(int maxResults, int firstResult) {
        return findCandidatEntities(false, maxResults, firstResult);
    }

    private List<Candidat> findCandidatEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(Candidat.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Candidat findCandidat(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Candidat.class, id);
        } finally {
            em.close();
        }
    }

    public int getCandidatCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<Candidat> rt = cq.from(Candidat.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
